package com.doyled.jump.scene;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;

import com.doyled.jump.framework.ResourceManager;
import com.doyled.jump.framework.SceneManager;
import com.doyled.jump.menuitem.ExitButton;
import com.doyled.jump.menuitem.LeftButton;
import com.doyled.jump.menuitem.RightButton;
import com.doyled.jump.menuitem.StartButton;
import com.doyled.jump.sound.SoundPlayer;

import java.awt.CardLayout;
import java.util.LinkedList;

public class MenuScene extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4279881540586802853L;
	// private SoundPlayer sound;
	private Image menuBackgroundImg;
	private LinkedList<String> functionName; // function name of game
	private int numberOfFunc; // number of function
	private int currentFunc; // current function id

	/**
	 * Create the panel.
	 */
	public MenuScene() {
		menuBackgroundImg = ResourceManager.getInstance()
				.getMenuBackGroundImage();
		setLayout(new BorderLayout(0, 0));

		functionName = new LinkedList<String>();

		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);

		JPanel funcPanel = new JPanel();
		funcPanel.setLayout(new CardLayout(0, 0));

		JButton btnStart = new StartButton();
		btnStart.addActionListener(new ButtonProcessing(funcPanel, "Start"));
		funcPanel.add(btnStart, "Start");
		numberOfFunc++;
		functionName.add("Start");

		JButton btnExit = new ExitButton();
		btnExit.addActionListener(new ButtonProcessing(funcPanel, "Exit"));
		funcPanel.add(btnExit, "Exit");
		numberOfFunc++;
		functionName.add("Exit");

		JButton btnLeft = new LeftButton();
		btnLeft.addActionListener(new ButtonProcessing(funcPanel, "Left"));

		JButton btnRight = new RightButton();
		btnRight.addActionListener(new ButtonProcessing(funcPanel, "Right"));

		panel.add(btnLeft);
		panel.add(funcPanel);
		panel.add(btnRight);

		// sound = new SoundPlayer("/audio/Test3.wav");
		// sound.play();
	}

	@Override
	public void paint(Graphics g) {
		g.drawImage(menuBackgroundImg, 0, 0, this);
		super.paintComponents(g);
	}

	class ButtonProcessing implements ActionListener {

		private String buttonName;
		JPanel funcPanel;

		public ButtonProcessing(JPanel funcPanel, String buttonName) {
			this.buttonName = buttonName;
			this.funcPanel = funcPanel;
		}

		@Override
		public void actionPerformed(ActionEvent evt) {
			// change scene processing
			if (buttonName.equals("Start")) {
				ChoosePlayersScene choosePlayers = new ChoosePlayersScene();
				choosePlayers.setVisible(true);
				if (choosePlayers.isOk()) {	//change to game scene if user finished setting user information
					SceneManager.getInstanse().setGameScene("Game Scene");
				}
				return;
			} else if (buttonName.equals("Exit")) {
				System.exit(0);
			}

			// arrow processing
			int nextFunc = 0;
			if (buttonName.equals("Left")) {
				nextFunc = (currentFunc + numberOfFunc - 1) % numberOfFunc; // get
																			// next
																			// function
																			// Id
			} else {
				nextFunc = (currentFunc + numberOfFunc + 1) % numberOfFunc; // get
																			// next
																			// function
																			// Id
			}

			CardLayout cardLayout = (CardLayout) funcPanel.getLayout();
			cardLayout.show(funcPanel, functionName.get(nextFunc));

			currentFunc = nextFunc;
		}
	}
}
